/*-----------------------------------
There are two fields in Usercontrol table for representing user rights,
they are userrights and userrights2. The data type of userrights in
database is char(16) and the data type in java code is long. Two methods
in java.lang.Long class are used to convert long to char and backward.
Using Long.toHexString(long i) to convert a long value to a Hex String
and using Long.parseLong(String s, int radix) to convert a Hex string to
a long value.

Each long value has 64 bits. Each module in this system will have 4 bits
to present the user privileges of the module. "0" and "1" present if user
has the privilege or not. "0" means "has not" and "1" means "has". In this
way, we use different bit to present different privilege. For example,
there are two kind of privileges of download module. One is normal user
privilege, the other is admin privilege. "0001" means the user has normal
user privilege and "0010" means the user has admin privilege.

The "userrights" is using to present the privileges of current modules and
the "userrights2" is reserved for future modules. Right now, we have news,
calendar, poll, resource, reservation, todo list, address book, download,
bbs, user management, totally 10 modules. The privilege bits of userrights
are arranged as following:

bit 1 - 4 : news
bit 5 - 8 : calendar
bit 9 - 12 : poll
bit 13 - 16 : resource
bit 17 - 20 : reservation
bit 21 - 24 : todo list
bit 25 - 28 : address book
bit 29 - 32 : download
bit 33 - 36 : bbs
bit 37 - 40 : user management
bit 41 - 44 : bookmark

bit 57 - 64 : for system use

------------------------------------------*/

package linuxbox.admin.logic;

public class UserRights {
  public final static long SYSTEM_ADMIN =     0x0004000000000000L;
  public final static long NOMAL_USER =       0x0001000000000000L;
  //public final static long ADMIN_USER =       0x0002000000000000L;

  public final static long NEWS_ADMIN =       0x0000000000000001L;
  public final static long CALEN_ADMIN =      0x0000000000000010L;
  public final static long POLL_ADMIN =       0x0000000000000100L;
  public final static long RESOURCE_ADMIN =   0x0000000000001000L;
  public final static long RESERV_ADMIN =     0x0000000000010000L;
  public final static long TODO_ADMIN =       0x0000000000100000L;
  public final static long ADDR_ADMIN =       0x0000000001000000L;
  public final static long DOWNLOAD_ADMIN =   0x0000000010000000L;
  public final static long BBS_ADMIN =        0x0000000100000000L;
  public final static long USER_ADMIN =       0x0000001000000000L;
  public final static long BOOKMARK_ADMIN =   0x0000010000000000L;

  public final static long FULL_ADMIN =       0x0004001111111111L;

  public UserRights() {

  }

  public static long mergeRights(long[] rights) {
    long result = 0;

    if (rights != null) {
      for (int i = 0; i < rights.length; i++) {
        result = result | rights[i];
      }
    }

    return result;
  }

  public static long mergeRight(long right1, long right2) {
    return right1 | right2;
  }

  public static long removeRight(long right1, long right2) {
    right2 = ~right2;
    return right1 & right2;
  }

  public static boolean checkRight(long checking, long checked) {
    long result = checking & checked;
    if (result == 0)
      return false;
    else
      return true;
  }

  public static boolean isSystemAdmin(long right) {
        return checkRight(right, SYSTEM_ADMIN);
  }

  public static boolean isAnyAdmin(long right) {
        return checkRight(right, FULL_ADMIN);
  }

    public static boolean isNewsAdmin(long right) {
        return checkRight(right, NEWS_ADMIN);
    }

    public static boolean isPollAdmin(long right) {
        return checkRight(right, POLL_ADMIN);
    }

    public static boolean isResourceAdmin(long right) {
        return checkRight(right, RESOURCE_ADMIN);
    }

    public static boolean isReservationAdmin(long right) {
        return checkRight(right, RESERV_ADMIN);
    }

    public static boolean isDownloadAdmin(long right) {
        return checkRight(right, DOWNLOAD_ADMIN);
    }

    public static boolean isBBSAdmin(long right) {
        return checkRight(right, BBS_ADMIN);
    }

    public static boolean isUserAdmin(long right) {
        return checkRight(right, USER_ADMIN);
    }

    public static boolean isTodoAdmin(long right) {
        return checkRight(right, TODO_ADMIN);
    }

    public static boolean isBookmarkAdmin(long right) {
        return checkRight(right, BOOKMARK_ADMIN);
    }

    public static long setSystemAdmin(long right) {
        return mergeRight(right, SYSTEM_ADMIN);
    }

    public static long setNewsAdmin(long right) {
        return mergeRight(right, NEWS_ADMIN);
    }

    public static long setPollAdmin(long right) {
        return mergeRight(right, POLL_ADMIN);
    }

    public static long setResourceAdmin(long right) {
        return mergeRight(right, RESOURCE_ADMIN);
    }

    public static long setReservationAdmin(long right) {
        return mergeRight(right, RESERV_ADMIN);
    }

    public static long setDownloadAdmin(long right) {
        return mergeRight(right, DOWNLOAD_ADMIN);
    }

    public static long setBBSAdmin(long right) {
        return mergeRight(right, BBS_ADMIN);
    }

    public static long setUserAdmin(long right) {
        return mergeRight(right, USER_ADMIN);
    }

    public static long setTodoAdmin(long right) {
        return mergeRight(right, TODO_ADMIN);
    }

    public static long setBookmarkAdmin(long right) {
        return mergeRight(right, BOOKMARK_ADMIN);
    }

    public static long unsetSystemAdmin(long right) {
        return removeRight(right, SYSTEM_ADMIN);
    }

    public static long unsetNewsAdmin(long right) {
        return removeRight(right, NEWS_ADMIN);
    }

    public static long unsetPollAdmin(long right) {
        return removeRight(right, POLL_ADMIN);
    }

    public static long unsetResourceAdmin(long right) {
        return removeRight(right, RESOURCE_ADMIN);
    }

    public static long unsetReservationAdmin(long right) {
        return removeRight(right, RESERV_ADMIN);
    }

    public static long unsetDownloadAdmin(long right) {
        return removeRight(right, DOWNLOAD_ADMIN);
    }

    public static long unsetBBSAdmin(long right) {
        return removeRight(right, BBS_ADMIN);
    }

    public static long unsetUserAdmin(long right) {
        return removeRight(right, USER_ADMIN);
    }

    public static long unsetTodoAdmin(long right) {
        return removeRight(right, TODO_ADMIN);
    }

    public static long unsetBookmarkAdmin(long right) {
        return removeRight(right, BOOKMARK_ADMIN);
    }

    public static String getUserRightsString(long rights) {
        String str = Long.toHexString(rights);
        return str;
    }

    public static long getUserRightsLong(String sRights)
    throws NumberFormatException {
        long l = Long.parseLong(sRights, 16);
        return l;
    }

    public static void main(String[] args) {
        String str = getUserRightsString(SYSTEM_ADMIN);
        System.out.println("right is [" + str + "]");
    }
}
